package regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA
 * Description:正则捕获分组
 * User:22954
 * Date:2024-05-29
 * Time:10:42
 */
public class Rule5 {
    /*
    (pattern)----非命名捕获  整个正则匹配文本储存编号为0,按组捕获按括号顺序从1编号
    (?<name>pattern)----命名捕获 将捕获的组命名,可根据组名查询    name字符串不能数字开头,不能包含标点符号
     */
    public static void main(String[] args) {
        String content = "ac1234 ./9876dd";
        String regex = "(\\d\\d)(\\d\\d)";
        regex = "(?<g1>\\d\\d)(?<g2>\\d\\d)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while(matcher.find()){
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
            System.out.println(matcher.group("g1"));
            System.out.println(matcher.group(2));
            System.out.println(matcher.group("g2"));
        }
    }
}
